package com.kaikeba.wx.controller;

import com.kaikeba.bean.*;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.AdminService;
import com.kaikeba.service.ExpressmanService;
import com.kaikeba.service.UserService;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.RandomUtil;
import com.kaikeba.util.SMSUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserController {
    @ResponseBody(value = "/wx/sendSMS.do")
    public String sendSMS(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("phone");
//        String code = RandomUtil.getCode()+"";
//        boolean b = SMSUtil.loginSMS(phone, code);
        String code = "123456";
        boolean b = true;
        Message msg = new Message();
        if(b){
            msg.setStatus(1);
            msg.setResult("短信发送成功，请查收");
            UserUtil.setLoginSMS(req.getSession(), phone, code);
        }else{
            msg.setStatus(-1);
            msg.setResult("短信发送失败");
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/wx/login.do")
    public String login(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("phone");
        String userCode = req.getParameter("code");
        String sysCode = UserUtil.getLoginCode(req.getSession(), phone);
        Message msg = new Message();
        if(sysCode == null || sysCode == ""){
            msg.setStatus(-2);
            msg.setResult("手机号未获取过验证码");
        }else if(!sysCode.equals(userCode)){
            msg.setStatus(-1);
            msg.setResult("验证码错误");
        }else{
            Expressman expressman = null;
            User user = null;
            if((expressman = ExpressmanService.findByPhone(phone))!=null){
                //是快递员
                UserUtil.setWxExpressman(req.getSession(), expressman);
                msg.setStatus(2);
            }else if((user = UserService.findByPhone(phone))!=null){
                //是用户
                UserUtil.setWxUser(req.getSession(), user);
                msg.setStatus(1);
            }else{
                //都不是，这里对其进行用户注册
                user = new User();
                user.setPhone(phone);
                UserService.insert(user);
                UserUtil.setWxUser(req.getSession(), user);
                msg.setStatus(1);
            }
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/wx/isExpressman.do")
    public String isExpressman(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        Message msg = new Message();
        if(UserUtil.getWxExpressman(session) != null){
            //是快递员
            msg.setStatus(1);
        }else{
            //不是快递员
            msg.setStatus(0);
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/wx/logout.do")
    public String logout(HttpServletRequest req, HttpServletResponse resp){
        req.getSession().invalidate();
        Message msg = new Message();
        msg.setStatus(0);
        return JSONUtil.toJson(msg);
    }

}
